package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAudit;
import cn.wolfcode.business.mapper.BusAuditMapper;
import cn.wolfcode.business.service.BusAuditService;
import cn.wolfcode.business.service.BusServiceItemService;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static cn.wolfcode.business.domain.BusAudit.STATUS_CANCEL;
import static cn.wolfcode.business.domain.BusServiceItem.AUDITSTATUS_INIT;


@Service
public class BusAuditServiceImpl implements BusAuditService {
    @Autowired
    BusAuditMapper busAuditMapper;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    BusServiceItemService busServiceItemService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;


    @Override
    public List<BusAudit> getAuditList() {
        return busAuditMapper.getAuditList();
    }

    @Override
    public Integer insert(BusAudit busAudit) {
        return busAuditMapper.insert(busAudit);
    }

    @Override
    public Integer updateInstanceId(Long businessKey, String processInstanceId) {
        return busAuditMapper.updateInstanceId(businessKey, processInstanceId);
    }

    /**
     * 根据流程实例获取当前的流程进度图
     *
     * @param instanceId
     * @return
     */
    @Override
    public InputStream getAuditprogressPng(Long instanceId) {
        if (instanceId == null)
            throw new RuntimeException("非法参数！");
        //首先获取到流程定义的id

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(String.valueOf(instanceId)).singleResult();
        //当前流程已被撤销
        BpmnModel bpmnModel=null;
        List<String> activeActivityIds=new ArrayList<>();
        if (processInstance==null){
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId.toString()).singleResult();
            bpmnModel=repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
        }else{
             bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
            //获取活动节点的集合
            activeActivityIds = runtimeService.getActiveActivityIds(instanceId.toString());
        }
        DefaultProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
        InputStream inputStream = processDiagramGenerator.generateDiagram(bpmnModel,
                activeActivityIds,     //活动节点的集合
                Collections.emptyList(),    //活动连线的集合
                "宋体", "宋体", "宋体");
        return inputStream;
    }

    @Transactional
    @Override
    public Integer deleteCancelAudit(Long id) {
        //参数合理化校验
        if(id==null)
            throw new RuntimeException("非法参数！");
        /*
        撤销要干几件事？
        1.删除业务表中数据
        2.在服务项中将当前套餐的状态回调为初始化
        3.删除此流程实例
         */
        //1.根据id删除业务表中的数据
        //3.删除流程实例,根据id查询流程实例id
        //查询流程实例Id
        BusAudit busAudit = busAuditMapper.selectInstanceById(id);//根据id查询流程实例id
        runtimeService.deleteProcessInstance(busAudit.getInstanceId(),"撤销流程实例");
        busAuditMapper.deleteAuditById(id,STATUS_CANCEL);
        //2.将服务项中将当前套餐的状态回调为初始化
        return busServiceItemService.updateAuditStatus(id,AUDITSTATUS_INIT);
    }

}
